package com.wallen.timedsmssender;

import android.content.Context;
import android.media.Image;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;

import java.util.ArrayList;

public class PairAdapter extends ArrayAdapter<Pair<String, String>> {
    private final Context context;
    private final ArrayList<Pair<String,String>> values;

    public PairAdapter(Context context, ArrayList<Pair<String, String>> values) {
        super(context, R.layout.datapair, values);
        this.context = context;
        this.values = values;
    }

    @Override
    public View getView(final int position, final View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.datapair, parent, false);
        EditText key = (EditText) rowView.findViewById(R.id.key);
        final EditText value = (EditText) rowView.findViewById(R.id.value);
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon);
        String first = String.valueOf(values.get(position).first);
        String second = String.valueOf(values.get(position).second);
        if(first.isEmpty() && second.isEmpty()){
            imageView.setAlpha(0);
        }
        else {
            imageView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    values.remove(position);
                    notifyDataSetChanged();
                }
            });
        }
        key.setText(first);
        value.setText(second);
        key.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Pair p = new Pair(s, values.get(position).second);
                values.add(position, p);
                values.remove(position + 1);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        value.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Pair p = new Pair(values.get(position).first,s);
                values.add(position,p);
                values.remove(position+1);
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        return rowView;
    }
} 